package org.aiforum.backend.controller;

import java.util.List;

import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.service.UserService;
import org.aiforum.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 用户控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
   
	@Autowired
	private UserService userService;
	
	@RequestMapping(value={"/insert"},method={RequestMethod.POST})
	public ModelAndView insert(User user){
		//插入板块
		Response response=userService.insert(user);
		//创建视图模型对象
		ModelAndView modelAndView=new ModelAndView();
		//重定向到消息页
		modelAndView.setViewName("redirect:../message");
		modelAndView.addObject("message", response.getMessage());
		return modelAndView;
	}
	
	@RequestMapping(value={"/query"})
	public ModelAndView query(QueryPreperty queryPreperty,PageInfo page){
		//设置分页参数
		// PageHelper.startPage(当前页码, 每页显示的记录数量, 是否查询计算总页数);
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), true);
		List<User> users=userService.queryByPreperty(queryPreperty);
		page=new PageInfo<User>(users);
		
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("user/list");
		modelAndView.addObject("users",users);
		modelAndView.addObject("queryPreperty", queryPreperty);
		modelAndView.addObject("page", page);
		return modelAndView;
		
	}
	
	@RequestMapping(value={"/find/{userId}"},method={RequestMethod.GET})
	public ModelAndView find(@PathVariable Integer userId){
		 User user=userService.find(userId);
		 ModelAndView modelAndView=new ModelAndView();
		 modelAndView.setViewName("user/update");
		 modelAndView.addObject("user", user);
		 return modelAndView;
	}
	
	@RequestMapping(value={"/modify"},method={RequestMethod.POST})
	public ModelAndView modify(User user){
		//插入板块
		Response response=userService.update(user);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("redirect:../message");
		modelAndView.addObject("message", response.getMessage());
		return modelAndView;
	}
	
	@RequestMapping(value={"/delete/{userId}"},method={RequestMethod.GET})
	public ModelAndView delete(@PathVariable Integer userId){
		Response response=userService.delete(userId);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.setViewName("redirect:../../message");
		modelAndView.addObject("message",response.getMessage());
		return modelAndView;
   }
	
	@RequestMapping(value={"/checkExists"},method={RequestMethod.GET})
	@ResponseBody
	public Response exists(QueryPreperty queryPreperty){
		// 检查板块名称,代码,路径是否已经存在
		Response response=userService.checkExists(queryPreperty);
		return response;
	}
}
